﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.Windows;

namespace pallete_main
{

    //定义dwg文件的操作
    class module_dwg
    {

        //拷贝dwg文件元素到指定的位置
        public ObjectId CopyDWG(string sourcename, Point3d position)
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            ObjectId editId;

            if (!File.Exists(sourcename))
            {
                MessageBox.Show("文件不存在");
                return ObjectId.Null;
            }

            try
            {
                Database db = new Database(false, true);
                db.ReadDwgFile(sourcename, FileShare.Read, true, "");
                db.CloseInput(true);

                DocumentLock docLock = doc.LockDocument();
                Database destDb = doc.Database;

                using (Transaction trans = doc.TransactionManager.StartTransaction())
                {
                    string blockname = SymbolUtilityServices.GetBlockNameFromInsertPathName(sourcename);
                    ObjectId id = destDb.Insert(blockname, db, false);
                    BlockTable bt = trans.GetObject(doc.Database.BlockTableId, OpenMode.ForRead) as BlockTable;
                    BlockReference blockref = new BlockReference(position, id);
                    blockref.ScaleFactors = new Scale3d(1.0);
                    BlockTableRecord tbr = trans.GetObject(destDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
                    editId = tbr.AppendEntity(blockref);
                    trans.AddNewlyCreatedDBObject(blockref, true);
                    trans.Commit();
                    tbr.Dispose();
                    bt.Dispose();
                }

                docLock.Dispose();
                destDb.Dispose();
                db.Dispose();
            }
            catch (System.Exception ee)
            {
                return ObjectId.Null;
            }

            return editId;
        }

    }

    //基本图形参数定义
    class module_basic_graphics
    {
         //文字大小
        public double nTextHeight = 3; 
      //  public double dtkWidth;
       // public double dtkHeight;
        public double dtkBorderWidth = 20;
        public double dtkBorderHeight = 10;
        public double dtkBorderBottom = 30;
      //  public int ntkTableRowNum;
        public bool m_showLeaf = false ;
        public int m_descMaxNum = 5 ;
        public double dTableFontHeight = 1 ;
        public double dTableRowHeight = 1.5 ;
        public const double dTableRowWidth = 40;
        //每行设备数
        public int nDeviceLineNum = 10 ;
        //设备间隔距 离
        public const double dDeviceInterX = dTableRowWidth * 2 + 15 ;
        //设备间隔距离
        public const double dDeviceInterY = 250;
        //画线x距离
        public const int nLineDx = 3 ;
        //画线y距离
        public const int nLineDy = 3 ;
        public const int nIedDx = 10;
        public const int nIedDy = 30;
        //固定距离拉长
        public const int nPrivateMinue = 20 ;
        //初始图元偏移位置
        public int nInitDeviceDx = 400 ;
        //初始图元偏移位置
        public int nInitDeviceDy = 400 ;
        //Subleaf位置偏移,相对Device
        public const int nSubLeafDx = -50 ;
        //Publeaf位置偏移,相对SubLeaf
        public const int nPubLeafDx = 200 ;
        //初始第一次打开记录
        public bool bInitOpen = true ;
        //初始系统记录
        public bool bInitCheck = false ;

        //光缆参数
      //  public String m_glTkPath ;
        public double m_glTK_width = 0;
        public double m_glTK_height = 0;
        public double m_glTK_dx = 0;
        public double m_glTK_dy = 0;
        public double m_glTK_invalideW = 0;
        public double m_glTK_invalideH = 0;
        public double m_glTK_reservePer = 0;
    }
}
